package com.winhxd.b2c.product.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.condition.*;
import com.winhxd.b2c.common.domain.product.model.ProductBrand;
import com.winhxd.b2c.common.domain.product.model.ProductSpu;
import com.winhxd.b2c.common.domain.product.vo.*;
import com.winhxd.b2c.common.feign.product.ProductServiceClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.product.service.ProductService;
import com.winhxd.b2c.product.service.ProductStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author luosixiang
 */
@Api(value = "商品", tags = "product")
@RestController
public class ProductController implements ProductServiceClient {

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductStockService productStockService;

    @ApiOperation(value = "Web端-获取商品列表")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<PagedList<ProductListWebVO>> findProductListByPage(@RequestBody ProductCondition condition) {
        ResponseResult<PagedList<ProductListWebVO>> result = new ResponseResult();
        logger.info("Web端获取商品列表,入参：" + JsonUtil.toJSONString(condition));

        //回收站列表
        condition.setRecycleBin(1L);
        PagedList<ProductListWebVO> list = productService.findProductListByPage(condition);

        logger.info("Web端获取商品列表,返参：" + JsonUtil.toJSONString(list));
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "Web端-获取商品列表(商城组件，带图片)")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<PagedList<ProductListForAssemblyWebVO>> findProductListForAssemblyByPage(@RequestBody ProductCondition condition) {
        ResponseResult<PagedList<ProductListForAssemblyWebVO>> result = new ResponseResult();
        logger.info("Web端-获取商品列表(商城组件，带图片),入参：" + JsonUtil.toJSONString(condition));

        PagedList<ProductListForAssemblyWebVO> list = productService.findProductListForAssemblyByPage(condition);

        logger.info("Web端-获取商品列表(商城组件，带图片),返参：" + JsonUtil.toJSONString(list));
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "Web端-获取商品列表(回收站)")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<PagedList<ProductListWebVO>> findProductListForRecycleBin(@RequestBody ProductCondition condition) {
        ResponseResult<PagedList<ProductListWebVO>> result = new ResponseResult();
        logger.info("Web端获取商品列表,入参：" + JsonUtil.toJSONString(condition));
        //回收站列表
        condition.setRecycleBin(0L);
        PagedList<ProductListWebVO> list = productService.findProductListByPage(condition);

        logger.info("Web端获取商品列表,返参：" + JsonUtil.toJSONString(list));
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "Web端-通用商品列表")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<List<ProductSpu>> findProductList(@RequestBody ProductSpu model) {
        ResponseResult<List<ProductSpu>> result = new ResponseResult();
        logger.info("Web端-通用商品列表,入参：" + JsonUtil.toJSONString(model));

        List<ProductSpu> spuList = productService.findProductList(model);

        logger.info("Web端-通用商品列表,返参：" + spuList);
        result.setData(spuList);
        return result;
    }

    @ApiOperation(value = "Web端-批量操作商品(上下架、删除、还原)")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Integer> operateProducts(@RequestBody ProductCondition condition) {
        ResponseResult<Integer> result = new ResponseResult();
        logger.info("批量操作商品,入参：" + JsonUtil.toJSONString(condition));

        Integer count = productService.operateProducts(condition);

        logger.info("批量操作商品,返参：" + count);
        result.setData(count);
        return result;
    }


    @ApiOperation(value = "获取购物车商品sku列表")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Map<Long, ProductShopcarSkuVO>> findShopcarSkuList(@RequestBody ProductSkuListCondition condition) {
        logger.info("{} - 获取购物车商品sku列表, 参数：ProductSkuListCondition={}", "", condition);
        ResponseResult<Map<Long,ProductShopcarSkuVO>> result = new ResponseResult<>();
        Map<Long, ProductShopcarSkuVO> skuVOMap = productService.findShopcarSkuList(condition);
        result.setData(skuVOMap);
        return result;
    }

    @ApiOperation(value = "获取收藏夹商品信息列表")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Map<Long, ProductSpuSimpleVO>> findSimpleProductList(@RequestBody ProductSpuListCondition condition) {
        logger.info("{} - 获取收藏夹商品信息列表, 参数：ProductSpuListCondition={}", "", condition);
        ResponseResult<Map<Long,ProductSpuSimpleVO>> result = new ResponseResult<>();
        Map<Long, ProductSpuSimpleVO> simpleVOMap = productService.findSimpleProductList(condition);
        result.setData(simpleVOMap);
        return result;
    }

    @ApiOperation(value = "Web端-保存商品信息")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Integer> saveProduct(@RequestBody ProductVO condition) {
        ResponseResult<Integer> result = new ResponseResult();
        logger.info("保存商品信息,入参：" + JsonUtil.toJSONString(condition));

        Integer rst = productService.saveProduct(condition);

        logger.info("保存商品信息,返参：" + JsonUtil.toJSONString(rst));
        result.setData(rst);
        return result;
    }

    @ApiOperation(value = "WEB端—新增、编辑商品")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<ProductVO> addOrModifyProduct(@RequestBody ProductSaveOrUpdateCondition condition) {
        logger.info("{} - WEB端—新增、编辑商品, 参数：ProductSaveOrUpdateCondition={}", "", condition);
        ResponseResult<ProductVO> result = new ResponseResult<>();

        ProductVO vo = productService.toSave(condition);

        logger.info("{} - WEB端—新增、编辑商品, 返参：ProductVO={}", "", vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "创建订单减库存增销量")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Integer> operateSubProductStock(@RequestBody List<ProductStockSkuCondition> condition) {
        ResponseResult<Integer> result = new ResponseResult();
        logger.info("更新库存及销量,入参：" + JsonUtil.toJSONString(condition));

        Integer count = productStockService.operateProductStock(condition);
        logger.info("更新库存及销量记录数：" + count);
        result.setData(count);
        return result;
    }

    @ApiOperation(value = "取消订单增库存减销量")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Integer> operateAddProductStock(@RequestBody List<ProductStockSkuCondition> condition) {
        ResponseResult<Integer> result = new ResponseResult();
        logger.info("更新库存及销量,入参：" + JsonUtil.toJSONString(condition));

        Integer count = productStockService.operateProductStock(condition);
        logger.info("更新库存及销量记录数：" + count);
        result.setData(count);
        return result;
    }

    @Override
    public ResponseResult<List<ProductBrand>> selectAllBrand() {
        ResponseResult<List<ProductBrand>> result = new ResponseResult<>();

        List<ProductBrand> productBrandList = productService.findAllBrand();

        logger.info("{} - WEB端—查询所有商品品牌, 返参：ProductVO={}", "", productBrandList);
        result.setData(productBrandList);
        return result;
    }

    @ApiOperation(value = "更新上架商品在ES的：商品的价格方案以及商品基本信息")
    @ApiResponses({ @ApiResponse(code = BusinessCode.CODE_OK, message = "成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常"),
            @ApiResponse(code = BusinessCode.CODE_1007, message = "参数无效")})
    @Override
    public ResponseResult<Boolean> operateProducPriceAndInfo() {
        ResponseResult<Boolean> result = new ResponseResult();
        logger.info("更新ES商品基本信息开始+++");
        Boolean flag = productService.operateProducPriceAndInfo();
        logger.info("更新ES商品基本信息结束+++，返参结果为：flag={}", flag);
        result.setData(flag);
        return result;
    }

}
